package com.cooldatasoft.web.cms.main;

import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;

import com.cooldatasoft.app.WOCApplication;
import com.cooldatasoft.data.Node;
import com.cooldatasoft.menu.AdminNodeBox;

public class AdminOrganizationChart extends WebPage{

	public AdminOrganizationChart() {
		this(1);
	}

	public AdminOrganizationChart(final Integer rootNodeId) {
		initPage(rootNodeId);
	}

	private void initPage(final Integer rootNodeId) {
		
		//Repeater with td
		List<Node> childrenNodeList = WOCApplication.get().getNodeServiceProxy().getChildrenNodeList(rootNodeId);
		boolean isLeafNode=false;
		if(childrenNodeList.size()==0){
			isLeafNode = true;
		}
		
		Link homeLink = new Link("homeLink") {
			@Override
			public void onClick() {
				if(rootNodeId==1){
					return;
				}
				setResponsePage(new AdminOrganizationChart());
			}
		}; 		
		add(homeLink);
		
		
		Link parentLink = new Link("parentLink") {
			@Override
			public void onClick() {
				if(rootNodeId==1){
					return;
				}
				Node rootNode = WOCApplication.get().getNodeServiceProxy().get(rootNodeId);
				setResponsePage(new AdminOrganizationChart(rootNode.getParentId()));
			}
		}; 		
		add(parentLink);
		
		AdminNodeBox rootNodeBox = new AdminNodeBox("rootNodeBox",rootNodeId,isLeafNode);
		rootNodeBox.setOutputMarkupId(true);
		add(rootNodeBox);
		
		ListView childrenNodeBoxes = new ListView("childrenNodeBoxes", childrenNodeList) {
		    protected void populateItem(ListItem item) {
		       final Node currentNode = (Node) item.getModelObject();
		       AdminNodeBox adminNodeBox = new AdminNodeBox("memberNodeBox",currentNode.getId(),true);
		       
		       item.add(adminNodeBox);				
		    }
		};
		add(childrenNodeBoxes);
	}

}
